﻿/*
 * Test classes for SSAS Partition Manager 
 * https://ssaspartitionmanager.codeplex.com/
 * 
 * Written by Dr. John Tunnicliffe of Decision Analytics, May 2015
 * eMail: john@decision-analytics.co.uk 
 * http://www.decision-analytics.co.uk/
 * https://www.linkedin.com/in/drjohntunnicliffe
 * 
 * Tests written with FluentAssertions, XUnit and Moq which can be installed from Nuget.  
 * 
 * FluentAssertions
 * http://www.fluentassertions.com/ 
 * 
 * XUnit
 * http://xunit.github.io/
 * 
 * Moq
 * https://github.com/Moq/moq4
 */

namespace AmoExtensions.UnitTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using AmoExtensions;
    using FluentAssertions;
    using Xunit;
    using Moq;

    public class UnitTestPartitionRepository
    {
        #region Constructor Tests
        [Fact]
        public void Constructor_NullConnectionStringThrowsException()
        {
            Action action = () => new PartitionRepository(null, null, null);
            action
                .ShouldThrow<ArgumentNullException>();
        }

        [Fact]
        public void Constructor_EmptyConnectionStringThrowsException()
        {
            Action action = () => new PartitionRepository("     ", null, null);
            action
                .ShouldThrow<ArgumentNullException>();
        }

        [Fact]
        public void Constructor_NullSqlThrowsException()
        {
            Action action = () => new PartitionRepository("select 1", null, null);
            action
                .ShouldThrow<ArgumentNullException>();
        }

        [Fact]
        public void Constructor_EmptySqlThrowsException()
        {
            Action action = () => new PartitionRepository("select 1", "     ", null);
            action
                .ShouldThrow<ArgumentNullException>();
        }

        #endregion

        #region SsasServerName
        [Fact]
        public void SsasServerName_ShouldBeNull()
        {
            IPartitionRepository pr = MockObjects.GetMockedRepository();
            string serverName = pr.SsasServerName("DEV");

            serverName.Should().BeNull();
        }
        #endregion

        #region ListPartitions
        [Fact]
        public void ListPartitions_ShouldReturnEnumerableList()
        {
            IAmoServer server = MockObjects.GetMockedConnectedServer();
            IPartitionRepository pr = MockObjects.GetMockedRepository();
            IEnumerable<IPartition> partitionList = pr.GetListOfPartitions(server);
            partitionList
                .Should()
                .BeOfType<List<IPartition>>();
        }

        [Fact]
        public void ListPartitions_ShouldReturnZeroItems()
        {
            IAmoServer server = MockObjects.GetMockedConnectedServer();
            IPartitionRepository pr = MockObjects.GetMockedRepository();
            IEnumerable<dynamic> partitionList = pr.GetListOfPartitions(server);
            partitionList.Count().Should().Be(0);              
        }

        [Fact]
        public void ListPartitions_ShouldReturnOneItems()
        {
            IAmoServer server = MockObjects.GetMockedConnectedServer();
            IPartitionRepository pr = MockObjects.GetMockedPopulatedRepository(server);
            IEnumerable<IPartition> partitionList = pr.GetListOfPartitions(server);
            partitionList.Count().Should().Be(1);
        }
        #endregion
    }
}
